﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

using Common;

namespace DispatcherLibrary
{
	/// <summary>
	/// Интерфейс сервиса диспетчера
	/// </summary>
	[ServiceContract(CallbackContract = typeof(IDispatcherCallback))]
	public interface IDispatcher
	{
		/// <summary>
		/// Авторизация пользователя
		/// </summary>
		/// <param name="name">Логин пользователя</param>
		/// <param name="passwrod">Пароль</param>
		/// <returns>Информация об успешности авторизации</returns>
		[OperationContract(IsOneWay = false)]
		ReturnInformation Autorization(string name, string passwrod);

		/// <summary>
		/// Возращает список текущих игр
		/// </summary>
		/// <returns>Список текущих игр</returns>
		[OperationContract(IsOneWay = false)]
		Games GetCurrentGames();

		[OperationContract(IsOneWay = false)]
		Users GetAllUsers();

		/// <summary>
		/// Создание новой игры
		/// </summary>
		/// <param name="gameName">Название игры</param>
		/// <param name="userName">Имя пользователя</param>
		/// <returns>Информация об успешности создания</returns>
		[OperationContract(IsOneWay = false)]
		void CreateGame(string gameName, string user1Name, string user2Name);

		[OperationContract(IsOneWay = false)]
		ReturnInformation SpectateGame(string gameName, string userName);

		[OperationContract(IsOneWay = true)]
		void Subscribe();

		[OperationContract(IsOneWay = true)]
		void Unsubscribe();

		[OperationContract(IsOneWay = true)]
		void SendMessage(string msg);

		[OperationContract(IsOneWay = true)]
		void JoinLobby(string userName);

		[OperationContract(IsOneWay = true)]
		void LeaveLobby(string userName);

		[OperationContract(IsOneWay = false)]
		void InviteToGame(string userName, string invitedUserName);

		[OperationContract(IsOneWay = true)]
		void RegisterServer(string serverAddress);

		[OperationContract(IsOneWay = true)]
		void UnregisterServer(string serverAddress);

		//реалтзовать потом
		//[OperationContract(IsOneWay = true)]
		//void GameEnded(string userName);
	}

	public interface IDispatcherCallback
	{
		[OperationContract(IsOneWay = true)]
		void ReceivedMessage(string msg);

		[OperationContract(IsOneWay = true)]
		void UserJoined(string userName);

		[OperationContract(IsOneWay = true)]
		void UserLeft(string userName);

		[OperationContract(IsOneWay = true)]
		void Invited(string userName, string invitedUserGame);

		[OperationContract(IsOneWay = true)]
		void InviteAccepted(GameInfo gameInfo);
	}

	/// <summary>
	/// Класс, возращаемый после определённый действий, выполняемых диспетчером
	/// </summary>
	[DataContract]
	public class ReturnInformation
	{
		bool _successful = true;
		string _errorMsg = "";
		GameInfo _info = null;

		/// <summary>
		/// Успешность действия
		/// </summary>
		[DataMember]
		public bool Success
		{
			get { return _successful; }
			set { _successful = value; }
		}

		/// <summary>
		/// Информация об ошибке
		/// </summary>
		[DataMember]
		public string ErrorMsg
		{
			get { return _errorMsg; }
			set { _errorMsg = value; }
		}

		[DataMember]
		public GameInfo Info
		{
			get { return _info; }
			set { _info = value; }
		}
	}

	[DataContract]
	public class GameInfo
	{
		string _gameName = "";
		string _serverAddress = "";
		string _playerName = "";
		Player _player;
		Guid _gameGuid;

		[DataMember]
		public Guid GameId
		{
			get { return _gameGuid; }
			set { _gameGuid = value; }
		}

		[DataMember]
		public string GameName
		{
			get { return _gameName; }
			set { _gameName = value; }
		}

		[DataMember]
		public string GameAddress
		{
			get { return _serverAddress; }
			set { _serverAddress = value; }
		}

		[DataMember]
		public Player PlayerType
		{
			get { return _player; }
			set { _player = value; }
		}

		[DataMember]
		public string PlayerName
		{
			get { return _playerName; }
			set { _playerName = value; }
		}
	}


	/// <summary>
	/// Созданная игра
	/// </summary>
	[DataContract]
	public class Game
	{
		string _creator;
		string _name;

		/// <summary>
		/// Имя пользователя, создавшего игру
		/// </summary>
		[DataMember]
		public string Creator
		{
			get { return _creator; }
			set { _creator = value; }
		}

		/// <summary>
		/// Название игры
		/// </summary>
		[DataMember]
		public string Name
		{
			get { return _name; }
			set { _name = value; }
		}
	}

	/// <summary>
	/// Класс, содержащий список игр
	/// </summary>
	[DataContract]
	public class Games
	{
		List<Game> _games;

		/// <summary>
		/// Список игр
		/// </summary>
		[DataMember]
		public List<Game> Items
		{
			get { return _games; }
			set { _games = _games; }
		}
	}

	[DataContract]
	public class Users
	{
		List<string> _users;

		[DataMember]
		public List<string> Items
		{
			get { return _users; }
			set { _users = value; }
		}
	}


}